package com.yinhe.util.jibeiSync;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yinhe.model.jibeiPO.SCCOUT_PO;
import com.yinhe.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName redisListJson
 * @Description
 * @Author xiaolongZzz66
 * @Time 2023/12/7 20:21
 * @Version 1.0
 */


public class redisListJsonUtil {

    /**
     * 序列化对象
     * @param list
     * @return
     * @param <T>
     * @throws JsonProcessingException
     */
    public static <T> List<String> objectTOJson(List<T> list) throws JsonProcessingException {
        if (list==null) return null;
        List<String> jsonList = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        for (T po : list) {
            // 将自定义对象序列化为JSON字符串
            String jsonString = objectMapper.writeValueAsString(po);
            jsonList.add(jsonString);
        }
        return jsonList;
    }


    /**
     * 反序列化
     * @param list
     * @param type
     * @return
     * @param <T>
     * @throws JsonProcessingException
     */
    public static <T> List<T> jsonToObject(List<String> list,Class<T> type) throws JsonProcessingException {
        if (list==null) return null;
        List<T> objList = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        for (String str : list) {
            // 将自定义对象序列化为JSON字符串
            T jsonString = objectMapper.readValue(str,type);
            objList.add(jsonString);
        }
        return objList;
    }

}
